package com.example.picturemodule

import android.app.Activity
import android.graphics.Bitmap.CompressFormat
import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.Toast
import java.io.FileNotFoundException

/*
Bitmap: 加载一张图片数据到内存中, 都可以封装成一个Bitmap对象
	需求1: 加载资源文件中的图片资源并显示
	需求2: 加载存储空间中的图片资源并显示
	需求3: 将一个bitmap对象保存到存储空间中
*/
class BitmapTestActivity : Activity() {
	private  val TAG = "BitmapTestActivity"
	private var iv_bitmap1: ImageView? = null
	private var iv_bitmap2: ImageView? = null
	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_bitmap)
		iv_bitmap1 = findViewById<View>(R.id.iv_bitmap1) as ImageView
		iv_bitmap2 = findViewById<View>(R.id.iv_bitmap2) as ImageView

		//需求1: 加载资源文件中的图片资源并显示
		iv_bitmap1!!.setImageResource(R.drawable.ic_launcher_foreground)

		//需求2: 加载存储空间中的图片资源并显示
		val externalFilesDir = externalCacheDir
		Log.d(TAG, "${externalFilesDir?.path}")
		Toast.makeText(this,externalFilesDir?.path,Toast.LENGTH_LONG).show()
		val bitmap = BitmapFactory.decodeFile("/storage/sdcard/ic_launcher.png")
		iv_bitmap2!!.setImageBitmap(bitmap)
	}

	@Throws(FileNotFoundException::class)
	fun saveImage(v: View?) {
		// 需求3: 将一个bitmap对象保存到存储空间中
		val bitmap = BitmapFactory.decodeFile("/storage/sdcard/ic_launcher.png")
		bitmap.compress(CompressFormat.PNG, 100, openFileOutput("ic_launcher.png", MODE_PRIVATE))
		Toast.makeText(this, "保存完成", Toast.LENGTH_SHORT).show()
	}
}